﻿/*
 * This file is a part of the open source project http://meshsharp.codeplex.com
 * 
 * Author: Vladimir Vasiliev
 */
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MeshSharp.Core.Models;
using MeshSharp.Core;
using System.Xml.Serialization;
using System.IO;

namespace PathFinder2d
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            data.AddPoints();
            font = this.Font;
        }

        Data data = new Data();
        Vector3 curr;
        int[] last = new int[3] { -1, -1, -1 };
        int index = 0;

        Font font;

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;

            data.Draw(g, Font);

            if (path != null)
            {
                for (int i = 1; i < path.Count; ++i)
                {
                    g.DrawLine(Pens.Red, path[i - 1].X, path[i - 1].Y, path[i].X, path[i].Y);
                }
            }
        }

        static float GetDist(Point p, Vector3 v)
        {
            float rex = p.X - v.X;
            float rey = p.Y - v.Y;
            return rex*rex + rey*rey;
        }

        private void Form1_MouseDown(object sender, MouseEventArgs e)
        {
            List<Vector3> allPoints = data.AllPoints;
            for (int i = 0; i < allPoints.Count; ++i)
            {
                if (GetDist(e.Location, allPoints[i]) <= 9)
                {
                    curr = allPoints[i];
                    last[index++] = i - 2;
                    label1.Text = "{" + last[0] + "}, {" + last[1] + "}, {" + last[2] + "}";
                    if (index >= 3)
                        index = 0;
                    return;
                }
            }
        }

        private void Form1_MouseUp(object sender, MouseEventArgs e)
        {
            curr = null;
        }

        private void Form1_MouseMove(object sender, MouseEventArgs e)
        {
            object obj = curr;
            if (obj != null)
            {
                curr.X = e.X;
                curr.Y = e.Y;
                Invalidate();
            }

        }

        List<Vector3> path;

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                path = data.GetPath();
            }
            catch(Exception ee)
            {
                path = null;
                Exception eee = ee;
                string msg = string.Empty;
                while (eee != null)
                {
                    msg += eee.Message + "\n";
                    eee = eee.InnerException;
                }
                MessageBox.Show(msg, "Error");
            }
            Invalidate();
        }

        private void Form1_DoubleClick(object sender, EventArgs e)
        {
            MouseEventArgs ee = e as MouseEventArgs;
            if (ee != null)
            {
                data.Add(new Vector3(ee.X, ee.Y, 0));
                Invalidate();
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < last.Length; ++i)
            {
                if (last[i] == -1)
                {
                    MessageBox.Show("Select three vertexes");
                    return;
                }
                for (int j = i + 1; j < last.Length; ++j)
                    if (last[i] == last[j])
                    {
                        MessageBox.Show("Select distinct vertexes");
                        return;
                    }
            }
            data.Indices.AddRange(last);
            Invalidate();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            using (OpenFileDialog dlg = new OpenFileDialog())
            {
                if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    using (FileStream stream = new FileStream(dlg.FileName, FileMode.Open, FileAccess.Read))
                    {
                        data = (Data)serializer.Deserialize(stream);
                        label1.Text = string.Empty;
                        last[0] = -1;
                        last[1] = -1;
                        last[2] = -1;
                        data.Reload();
                        Invalidate();
                    }
                }
            }
        }

        static XmlSerializer serializer = new XmlSerializer(typeof(Data));

        private void button4_Click(object sender, EventArgs e)
        {
            using (SaveFileDialog dlg = new SaveFileDialog())
            {
                if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    using (FileStream stream = new FileStream(dlg.FileName, FileMode.Create, FileAccess.Write))
                    {
                        serializer.Serialize(stream, data);
                    }
                }
            }
        }
    }
}
